<?php
class Training_Model_User extends Zend_Db_Table{
	
	protected $_name = 'users';
	protected $_primary = 'id';
	//protected $_db;
	
	public function table2(){
		$select = $this->select()
					 ->setIntegrityCheck(false)
					 ->from('users AS u',array('id','user_name'))
					 ->join('user_group AS g', 'g.id = u.group_id',array('name as group_name'));
		echo '<br>' . $select;
		
		$result = $this->fetchAll($select);
	//	$result = $this->fetchAll();
		echo '<pre>';
		print_r($result);
		echo '</pre>';
	}
	
	public function setMode(){
		$db = $this->getAdapter();
		$db->setFetchMode(Zend_Db::FETCH_BOTH);
		
		$sql = 'SELECT * FROM users';
		$result = $db->fetchAssoc($sql);
		echo '<pre>';
		print_r($result);
		echo '</pre>';
	}
	
	public function fetch($options = 'all'){
		
		$db = $this->getAdapter();
		
		if($options ==  'one'){
			//$sql = 'SELECT id, user_name FROM users WHERE id > ? ';		
			$sql = 'SELECT COUNT(id) FROM users WHERE id > ? ';	
			$bind = array(12);
			$result = $db->fetchOne($sql,$bind);			
		}
		if($options ==  'row'){
			$sql = 'SELECT id, user_name FROM users WHERE id < ? ';			
			$bind = array(2);
			$result = $db->fetchRow($sql,$bind);
			
		}
		
		if($options ==  'pairs'){
			$sql = 'SELECT id, name FROM user_group';			
			$result = $db->fetchPairs($sql);
			$result[0] = ' -- Select a group -- ';
			ksort($result);
		}
		
		if($options ==  'col'){
			$sql = 'SELECT user_name, user_avatar, password FROM users WHERE id > ? AND status = ?';
			$bind = array(7,1);
			$result = $db->fetchCol($sql,$bind);
		}
		
		if($options ==  'all'){
			$sql = 'SELECT * FROM users WHERE id > ? AND status = ?';
			$bind = array(7,0);
			$result = $db->fetchAll($sql,$bind);
		}
		
		/*echo '<pre>';
		print_r($result);
		echo '</pre>';*/
		return $result;
	}
}